import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
import pytest



class Webutils:
    _driver = None

    @classmethod
    def get_driver(cls):
        # 先判断内存中是否存在操作句柄
        if cls._driver is None:
            # 如果没有操作句柄，得创建一个操作句柄，并且进行赋值与返回
            cls._driver = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
            return cls._driver

        else:
            # 如果存在操作句柄，直接返回
            return cls._driver

    @classmethod
    def close_driver(cls):
        # 判断内存中，是否存在操作句柄
        # 如果不为空，则退出
        if cls._driver is not None:
            cls._driver.quit()
            cls._driver = None
            pass


    #定位弹出功能
def get_current_msg(self, excepted):
    curr = self.driver.find_element(By.CSS_SELECTOR, '.layui-layer-content').text
    assert excepted in curr

def input_text(element,text):
    element.clear()
    element.send_keys(text)

def get_element(driver,style,value):
    driver.element=WebDriverWait(driver,2)
    element=driver.element.until(lambda driver:driver.find_element(by=style,value=value))
    return element


def is_location(driver,style,value):
    try:
        element=get_element(driver=driver,style=style,value=value)
    except Exception as e:
        print('当前规则，定位不到元素-----》', value)
        print(e)
    else:
        return element
    finally:
        print('好的')
